package com.qdxwx.data.service.subject;

import com.qdxwx.data.mapper.subject.SubjectMapper;
import com.qdxwx.models.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类Service业务层处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 查询分类
     *
     * @param subjectId 分类ID
     * @return 分类
     */
    @Override
    public Subject get(int subjectId) {
        if (subjectId<1) return null;
        return subjectMapper.selectById(subjectId);
    }

    /**
     * 查询分类
     *
     * @param subjectId 分类ID
     * @return 分类
     */
    @Override
    public String getName(int subjectId) {
        Subject subject = get(subjectId);
        if (subject == null) {
            return "未知";
        } else {
            return subject.getSubjectName();
        }
    }
    /**
     * 查询分类列表
     *
     * @return 分类
     */
    @Override
    public List<Subject> getList() {
        return subjectMapper.selectList();
    }
    /**
     * 新增分类
     *
     * @param subject 分类
     * @return 结果
     */
    @Override
    public int insert(Subject subject) {
        subject.setCreateTime(LocalDateTime.now());
        return subjectMapper.insert(subject);
    }

    /**
     * 修改分类
     *
     * @param subject 分类
     * @return 结果
     */
    @Override
    public int update(Subject subject) {
        return subjectMapper.update(subject);
    }
    /**
     * 删除分类信息
     *
     * @param subjectId 分类ID
     * @return 结果
     */
    @Override
    public int delete(int subjectId) {
        if (subjectId < 1) return 0;
        return subjectMapper.deleteById(subjectId);
    }

}
